package com.junzhiit.nengji.cms.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.junzhiit.nengji.cms.service.ChainSkuService;
import com.junzhiit.nengji.cms.web.exception.ChainNotFoundException;
import com.junzhiit.nengji.cms.web.exception.ShopNotFoundException;
import com.junzhiit.nengji.cms.web.exception.SkuNameIsExistException;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.vo.ChainSkuAddVO;
import com.junzhiit.nengji.cms.web.vo.ChainSkuListVO;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.cms.web.vo.SystemUserSessionVO;
import com.junzhiit.nengji.dao.ChainDAO;
import com.junzhiit.nengji.dao.ChainSkuDAO;
import com.junzhiit.nengji.dao.cache.SystemUserCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;
import com.junzhiit.nengji.model.Chain;
import com.junzhiit.nengji.model.ChainSku;
import com.junzhiit.nengji.model.Sku;

@Service
public class ChainSkuServiceImpl extends BaseServiceImpl implements ChainSkuService{
	
	@Autowired
	private ChainSkuDAO chainSkuDAO;
	
	@Autowired
	private ChainDAO chainDAO;
	
	@Autowired
	private SystemUserCacheDAO systemUserCacheDAO;
	
	@Override
	public Map<String, Object> findAll(DataTablesParamVO dtp, ChainSkuListVO chainSkuListVO) {

		ChainSku chainSku = new ChainSku();
		BeanUtils.copyProperties(chainSkuListVO, chainSku);

		List<Sku> list = chainSkuDAO.findAll(chainSku, dtp.getStart(), dtp.getLength());
		int totalCount = chainSkuDAO.getTotalCount(chainSku);
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}

	
	@Override
	public ResultVO getAll(String sessionId){
		ResultVO result=new ResultVO();
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(sessionId), SystemUserSessionVO.class);
		Chain chain = chainDAO.query(systemUserSessionVO.getSystemId());
		if (chain == null) {
			throw new ChainNotFoundException("连锁品牌不存在");
		}
		List<ChainSku> data=chainSkuDAO.queryAllSku(chain.getId());
		result.setData(data);
		
		return result;
	}
	
	
	@Transactional
	@Override
	public ResultVO add(ChainSkuAddVO chainSkuAddVO) {
		ResultVO result = new ResultVO();

		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(chainSkuAddVO.getSessionId()), SystemUserSessionVO.class);
		Chain chain = chainDAO.query(systemUserSessionVO.getSystemId());
		if (chain == null) {
			throw new ShopNotFoundException("连锁品牌不存在");
		}

		ChainSku chainSku = new ChainSku();
		chainSku.setName(chainSkuAddVO.getName());
		chainSku.setChainId(chain.getId());

		int count = chainSkuDAO.queryNameIsExit(chain.getId(), chainSku.getName());
		if (count > 0) {
			throw new SkuNameIsExistException("sku分类名字已经存在");
		}

		ChainSku data = chainSkuDAO.insert(chainSku);
		result.setData(data);
		return result;
	}
	
}
